package 队列;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/implement-stack-using-queues/
 */
public class _225_用队列实现栈 {

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        System.out.println(myStack.top());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
    }
}

class MyStack {

    private Queue<Integer> queue1 = new LinkedList<>();

    private Queue<Integer> queue2 = new LinkedList<>();

    public MyStack() {

    }

    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }

    public int pop() {
        return !queue1.isEmpty() ? queue1.poll() : -1;
    }

    public int top() {
        return !queue1.isEmpty() ? queue1.peek() : -1;
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}